//
//  DetailViewController.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "DetailViewController.h"

#import "RootViewController.h"

#import "AppDelegate.h"

#import "EnterViewController.h"

#import <AVOSCloud/AVOSCloud.h>

#import <EaseMob.h>

#import "SKXbirthdayViewController.h"

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UIButton *manBtn;
@property (weak, nonatomic) IBOutlet UIButton *womanBtn;

@property (weak, nonatomic) IBOutlet UITextField *nickName;


@property (weak, nonatomic) IBOutlet UIButton *userDate;

@property (nonatomic , strong)NSString *userGender;

@end

@implementation DetailViewController

-(instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        UIStoryboard *temp = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
        
        self = [temp instantiateViewControllerWithIdentifier:@"detailVC"];
        
        
    }
    return self;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.manBtn.layer.borderColor = [UIColor redColor].CGColor;
    
    self.manBtn.layer.borderWidth = 1;
    
    self.womanBtn.layer.borderColor = [UIColor cyanColor].CGColor;
    
    self.womanBtn.layer.borderWidth = 1;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveUserInfo)];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//选择性别

- (IBAction)chooseGender:(UIButton *)sender {
    
    self.manBtn.backgroundColor = [UIColor grayColor];
    
    self.womanBtn.backgroundColor = [UIColor grayColor];
    
    [self.womanBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    
     [self.manBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];

    
    [sender setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    sender.backgroundColor = [UIColor blueColor];
    
    self.userGender = sender.titleLabel.text;
    
}


//保存用户信息

-(void)saveUserInfo{
    
    NSLog(@"保存用户信息");
    
    EnterViewController *temp = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"enter"];
    
    temp.userNameText = self.userName;
    
    temp.userPwdText = self.userPasswd;
    
    //环信注册,成功跳转登录
    
    
    if (self.nickName.text.length!=0&&self.userGender.length!=0&&![self.userDate.titleLabel.text isEqualToString:@"自动匹配年龄和星座"]) {
    
    //leancloud注册
    
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    
        [user1 setObject:self.userName forKey:@"username"];
        [user1 setObject:self.userPasswd forKey:@"password"];
    
        [user1 setObject:self.nickName.text forKey:@"nickname"];

        [user1 setObject:self.userGender forKey:@"gender"];
        
    //新建用户
    AVUser *user = [AVUser user];
    //用户名
    user.username = self.userName;
    //密码
    user.password = self.userPasswd;
    //昵称
    [user setObject:self.nickName.text forKey:@"nickname"];
    //性别
    [user setObject:self.userGender forKey:@"gender"];
    //生日
    [user setObject:[user1 objectForKey:@"birthday"] forKey:@"birthday"];
    //签名
    [user setObject:[user1 objectForKey:@"constellation"] forKey:@"constellation"];
    
    [user setObject:[user1 objectForKey:@"age"] forKey:@"age"];
        
    [user setObject:[user1 objectForKey:@"myVoice"] forKey:@"myVoice"];
    
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            NSLog(@"环信注册成功");
            // 注册成功
            
            [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userName password:self.userPasswd withCompletion:^(NSString *username, NSString *password, EMError *error) {
                if (!error) {
                    
                    NSLog(@"环信注册成功");
                    
                    
                    
                    [self.navigationController pushViewController:temp animated:YES];
                    
                }else{
                    
                    NSLog(@"环信注册失败%@",error);
                    
                    //leancloud删除用户
                    [user deleteInBackground];
                    
                    
                }
                
            } onQueue:dispatch_get_main_queue()];
        } else {
            NSLog(@"Leancloud注册失败");
        }
    }];

    }
}

- (IBAction)chooseBrithday:(UIButton *)sender {
    
    SKXbirthdayViewController *temp = [[SKXbirthdayViewController alloc] init];
    
    temp.block = ^(NSString * birthday){
      
        [sender setTitle:birthday forState:UIControlStateNormal];
        
    };
    
    [self.navigationController pushViewController:temp animated:YES];
    
}



@end
